#coding:utf8
'''
Created on 2017/2/23
4:03
@author: Moon
qq : 550302989
'''


import datetime
import Feature

class TaskSchedulerManage(object):

    def __init__(self):
        # self.__Feature = __import__("Feature")
        # self.__feature = getattr(self.__Feature,"Feature")()
        self.__feature = Feature.Feature()

    # 添加指派任务（任务创建者管理员）
    def addAssignTask(self,assign_info):
        sql = "insert into taskassign(taskid,cmanid,mamberid,status,createtime) values(taskid='{taskid}',cmanid='{cmanid}',mamberid='{mamberid}',status='{status}',createtime='{createtime}')".format(
            taskid=assign_info['taskid'],cmanid=assign_info['cmanid'],mamberid=assign_info['mamberid'],status='未接受',createtime=datetime.datetime.now().strftime('%Y-%m-%d')
        )
        return self.__feature.dataCommitFeature(sql)

    # 查看所有指派的任务（任务创建者管理员）
    def findAssignTask(self,cmanid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskassign where cmanid='{cmanid}'".format(
            cmanid=cmanid
        )
        return self.__feature.findFeatureAll(sql)

    # 查看已确认接收指派的任务（任务创建者管理员）
    def determineAssignTask(self,cmanid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskassign where status=1 and cmanid='{cmanid}'".format(cmanid=cmanid)
        return self.__feature.findFeatureAll(sql)

    # 查看未确认指派的任务（任务创建者管理员）
    def nodetermineAssignTask(self,cmanid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskassign where status=0 and cmanid='{cmanid}'".format(cmanid=cmanid)
        return self.__feature.findFeatureAll(sql)

    # 修改指派任务（至高权限）
    def updateAssignTask(self,mamberid,assignid):
        sql = "update taskassign set mamberid='{mamberid}' where id='{assignid}'".format(mamberid=mamberid,assignid=assignid)
        return self.__feature.dataCommitFeature(sql)

    # 删除指派任务（至高权限）
    def deleteAssignTask(self,assignid):
        sql = "delete from taskassign where id='{assignid}'".format(assignid=assignid)
        return self.__feature.dataCommitFeature(sql)

    # 列出所有会员(员工)
    def findMamber(self):
        sql = "select id,name from users where authrank='3'"
        return self.__feature.findFeatureAll(sql)


    # 指派任务成功，发送短信或者email提醒
    # 这里暂时不写    指派任务，直接成功，不通过认证

    # 会员(员工)确认接收任务，确认完成，修改任务状态，任务开始
    # 这里暂时不写    指派任务，直接成功，不通过认证

    # 修改任务状态，任务开始
    def updateTaskStatus(self,status,taskid):
        sql = "update task set status='{status}' where id='{taskid}'".format(status=status,taskid=taskid)
        return self.__feature.dataCommitFeature(sql)

    # 任务管理员查看任务申请状况
    def findApplyTask(self,cmanid,taskid):
        sql = "select mamberid from taskapply where taskid='{taskid}' and cmanid='{cmanid}'".format(taskid=taskid,cmanid=cmanid)
        return self.__feature.findFeatureAll(sql)

    # 某会员(员工)添加申请接任务
    def addApplyTask(self,apply_info):
        sql = "insert into taskapply(taskid,cmanid,mamberid,status,createtime) " \
              "values(taskid='{taskid}',cmanid='{cmanid}',mamberid='{mamberid}',status='{status}',createtime='{createtime}') ".format(
            taskid=apply_info['taskid'],cmanid=apply_info['cmanid'],mamberid=apply_info['mamberid'],status='0',createtime=datetime.datetime.now().strftime('%Y-%m-%d')
        )
        return self.__feature.dataCommitFeature(sql)

    # 会员查看所有申请的任务
    def findMamberApplyTask(self,taskid,mamberid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskapply where taskid='{taskid}' and mamberid='{mamberid}'".format(taskid=taskid,mamberid=mamberid)
        return self.__feature.findFeatureAll(sql)

    # 会员查看未通过申请的任务
    def nodetermineApplyTask(self,mamberid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskapply where status=0 and mamberid='{mamberid}'".format(mamberid=mamberid)
        return self.__feature.findFeatureAll(sql)

    # 会员查看已通过申请的任务
    def determineApplyTask(self,mamberid):
        sql = "select id,taskid,cmanid,mamberid,status,createtime from taskapply where status=1 and mamberid='{mamberid}'".format(mamberid=mamberid)
        return self.__feature.findFeatureAll(sql)

    # 修改任务申请状态，任务开始
    def updateApplyTaskStatus(self,applyid):
        sql = "update taskapply set status=1 where id='{applyid}'".format(applyid=applyid)
        return self.__feature.dataCommitFeature(sql)

